package com.xkdx.guangguang.fragment.brand;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.BrandCommentary;
import com.xkdx.guangguang.shareclass.BrandCommentaryResult;
import com.xkdx.guangguang.shareclass.DiscountInfo;
import com.xkdx.guangguang.shareclass.Shop;

public class BrandInfoModule extends AbsModule {
	BrandCommentaryResult brandCommentary;
	List<Shop> partShopList;
	BrandInfo brandInfo;
	List<DiscountInfo> newsInfoList;

	@Override
	public void parseData() throws Exception {
		try {
			backJson = (JSONArray) result;

			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getBrandDetail")) {
					brandInfo = parseBrandInfo(jsonObj.getJSONObject("Content"));
				} else if (sortOfAct.equals("getInfoListByBrand")) {
					newsInfoList = parseNewProductInfoList(jsonObj
							.getJSONObject("Content"));
				} else if (sortOfAct.equals("getShopListByBrand")) {
					partShopList = parseBrandShopList(jsonObj
							.getJSONObject("Content"));
				} else {
					brandCommentary = parseBrandAllCommentInfo(jsonObj
							.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			// to do define a exception include all parseException
			throw e;
		}

	}

	private List<DiscountInfo> parseNewProductInfoList(JSONObject person)
			throws Exception {
		List<DiscountInfo> list = new ArrayList<DiscountInfo>();
		try {
			if (person == null) {
				return list;
			}
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray jsonArray = (JSONArray) stut.getJSONArray("ListInfo");
			JSONObject time = (JSONObject) stut.get("DetailInfo");

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = (JSONObject) jsonArray.opt(i);
				DiscountInfo shopNewProduct = new DiscountInfo();
				shopNewProduct.setInfoID(item.getString("InfoID"));
				shopNewProduct.setInfoTitle(item.getString("InfoTitle"));
				shopNewProduct.setInfoType(item.getString("InfoType"));
				shopNewProduct.setPictrueUrl(item.getString("PictureUrl"));
				shopNewProduct.setBeginTime(item.getString("BeginTime"));
				shopNewProduct.setEndTime(item.getString("EndTime"));
				shopNewProduct.setLatestInfoTime(time.getString("RefreshTime"));
				list.add(shopNewProduct);
			}

		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	private BrandInfo parseBrandInfo(JSONObject person) {
		BrandInfo brandInfo = new BrandInfo();
		try {
			JSONObject stuts = (JSONObject) person.get("Result");
			JSONObject item = stuts.getJSONObject("DetailInfo");
			JSONArray array = item.getJSONArray("PictureList");

			brandInfo.setAttentionCount(item.getString("AttentionCount"));
			brandInfo.setBrandID(Integer.valueOf(item.getString("BrandID")));
			brandInfo.setBrandName(item.getString("BrandName"));
			brandInfo.setIntro(item.getString("Intro"));
			brandInfo.setLogo(item.getString("Logo"));
			brandInfo.setPhone(item.getString("Phone"));
			brandInfo.setWebSiteUrl(item.getString("WebSiteUrl"));
			brandInfo.setWeiboUrl(item.getString("WeiboUrl"));
			brandInfo.setBrandNameEn(item.getString("BrandNameEn"));
			brandInfo.setBrandSimpleName(item.getString("BrandSimpleName"));
			List<String> brandLogolist = new ArrayList<String>();
			for (int i = 0; i < array.length(); i++) {
				String l = (String) array.getString(i);
				brandLogolist.add(l);
				brandInfo.setPictureList(brandLogolist);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return brandInfo;
	}

	private List<Shop> parseBrandShopList(JSONObject person) {
		ArrayList<Shop> list = new ArrayList<Shop>();
		try {
			JSONObject stuts = (JSONObject) person.get("Result");
			JSONArray jsonArray = (JSONArray) stuts.getJSONArray("ListInfo");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = (JSONObject) jsonArray.opt(i);
				Shop shop = new Shop();
				shop.setBrandID(item.getString("BrandID"));
				shop.setAddress(item.getString("Address"));
				shop.setLogo(item.getString("Logo"));
				shop.setDistance(item.getString("Distance"));
				shop.setLatitude(item.getString("Latitude"));
				shop.setLongitude(item.getString("Longitude"));
				shop.setSimpleIntro(item.getString("SimpleIntro"));
				shop.setShopID(item.getString("ShopID"));
				shop.setShopName(item.getString("ShopName"));
				shop.setShopType(item.getString("ShopType"));
				shop.setPhone(item.getString("Phone"));
				list.add(shop);

			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return list;
	}

	// 解析评论详情内容
	private BrandCommentaryResult parseBrandAllCommentInfo(JSONObject person) {
		BrandCommentaryResult comment = new BrandCommentaryResult();
		List<BrandCommentary> list = new ArrayList<BrandCommentary>();
		try {
			JSONObject stut = (JSONObject) person.get("Result");

			if (stut.has("DetailInfo")) {
				JSONObject time = stut.getJSONObject("DetailInfo");
				String lastTime = time.getString("RefreshTime");
				String dataCount = time.getString("DataCount");
				comment.setRefreshTime(lastTime);
				comment.setDataCount(dataCount);
			} else {
				comment.setDataCount("0");
			}
			if (stut.has("ListInfo")) {
				JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
				for (int i = 0; i < ListInfo.length(); i++) {
					JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
					BrandCommentary commentinfo = new BrandCommentary();
					commentinfo.setCommentContent(jsonObj
							.getString("CommentContent"));
					commentinfo.setNickName(jsonObj.getString("NickName"));
					commentinfo.setCreateTime(jsonObj.getString("CreateTime"));
					commentinfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
					commentinfo
							.setUserAvatars(jsonObj.getString("UserAvatars"));
					list.add(commentinfo);
				}
				comment.setBrandCommentList(list);
			}

		} catch (Exception e) {
		}

		return comment;
	}

	public class BrandInfo {
		private int BrandID;
		private String BrandName;
		private String Logo;
		private String Phone;
		private String WebSiteUrl;
		private String WeiboUrl;
		private String AttentionCount;
		private String Intro;
		private String BrandNameEn;
		private String BrandSimpleName;
		private List<String> PictureList = new ArrayList<String>();

		public BrandInfo() {
			super();
		}

			

		public int getBrandID() {
			return BrandID;
		}



		public void setBrandID(int brandID) {
			BrandID = brandID;
		}



		public String getBrandName() {
			return BrandName;
		}

		public void setBrandName(String brandName) {
			BrandName = brandName;
		}

		public String getLogo() {
			return Logo;
		}

		public void setLogo(String logo) {
			Logo = logo;
		}

		public String getPhone() {
			return Phone;
		}

		public void setPhone(String phone) {
			Phone = phone;
		}

		public String getWebSiteUrl() {
			return WebSiteUrl;
		}

		public void setWebSiteUrl(String webSiteUrl) {
			WebSiteUrl = webSiteUrl;
		}

		public String getWeiboUrl() {
			return WeiboUrl;
		}

		public void setWeiboUrl(String weiboUrl) {
			WeiboUrl = weiboUrl;
		}

		public String getAttentionCount() {
			return AttentionCount;
		}

		public void setAttentionCount(String attentionCount) {
			AttentionCount = attentionCount;
		}

		public String getIntro() {
			return Intro;
		}

		public void setIntro(String intro) {
			Intro = intro;
		}

		public String getBrandNameEn() {
			return BrandNameEn;
		}

		public void setBrandNameEn(String brandNameEn) {
			BrandNameEn = brandNameEn;
		}

		public String getBrandSimpleName() {
			return BrandSimpleName;
		}

		public void setBrandSimpleName(String brandSimpleName) {
			BrandSimpleName = brandSimpleName;
		}

		public List<String> getPictureList() {
			return PictureList;
		}

		public void setPictureList(List<String> pictureList) {
			PictureList = pictureList;
		}

	}

}
